// 异步数据-swiper

<template>
  <div class="icons">
    <swiper :options="swiperOption">
      <swiper-slide v-for="(slide, index) of swiperSlides" :key="index+'slide'">
        <div class="icon" v-for="(item,num) of slide" :key="num+'div'">{{item}}</div>
      </swiper-slide>
      <div class="swiper-pagination" slot="pagination"></div>
    </swiper>
  </div>
</template>

<script>
  export default {
    name: 'carrousel',
    data() {
      return {
        swiperOption: {
          pagination: '.swiper-pagination'
        },
        list: [1,2,3,4,5,6,7,8,9,10]
      }
    },
    computed:{
      swiperSlides(){ // 分页处理
        const pages = []
        this.list.forEach((item,index) => {
          const num = Math.floor(index/8)
          if(!pages[num]){
            pages[num] = []
          }
          pages[num].push(item)
        })
        return pages
      }
    },
    mounted() {
      setTimeout(() => {
        console.log('simulate async data')
        if (this.swiperSlides.length < 10) {
          this.swiperSlides.push(this.swiperSlides.length + 1)
        }
      }, 3000)
    }
  }
</script>

<style scoped lang="scss">
.icons >>> .swiper-container{
  padding-bottom:50%;
  height:0;
  .swiper-pagination{
    // background:red !important;
  }
  
}

.icon{
  position: relative;
  overflow: hidden;
  float:left;
  padding-bottom:25%; margin:.5%;
  width:24%; height:0;
  line-height: 7;  text-align: center;
  border:1px solid red;
  box-sizing:border-box;
  &:nth-child(even){
    background:#f90;
  }
}
</style>
